package spr.lime.lmAdm.controller.login;


import javax.servlet.http.HttpSession;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;




import spr.lime.lmAdm.model.login.AdmMemberModel;
import spr.lime.lmAdm.service.login.AdmLoginService;
import spr.lime.lmAdm.validator.login.AdmLoginValidator;

@Controller
@RequestMapping("/lmAdm")
public class AdmLoginController {
	
	
	private ApplicationContext context = new ClassPathXmlApplicationContext("/config/applicationContext.xml");
	//private AdmMemberModel hService = (AdmMemberModel) context.getBean("helloService");
	//어떤식으로든 top과 left 가 정해진다
	String ttop,left;
	
	
	
	
	@RequestMapping("/login.do")
	public String login() {		
		//return "/lmAdm/adm_login";
		return "lmAdm.login.page";
	}
	
	
	
	@RequestMapping(value="/loginProcess.do", method = RequestMethod.POST)
	public ModelAndView loginProcess(@ModelAttribute("LoginModel") AdmMemberModel loginModel, 
	BindingResult result, HttpSession session) {
		ModelAndView mav = new ModelAndView();
		
		new AdmLoginValidator().validate(loginModel, result);
		
		if(result.hasErrors()){
			mav.setViewName("/lmAdm/adm_login");
			return mav;
		}
		
		String uid = loginModel.getMe_uid();
		String pwd = loginModel.getMe_pwd();
		
		AdmLoginService loginService = (AdmLoginService) context.getBean("admloginService");
		AdmMemberModel loginCheckResult = loginService.loginCheck(uid,pwd);
		
		if(loginCheckResult == null){
			System.out.print("로그인실패 아이디 비번 안맞음 없음");
			mav.addObject("me_uid", uid);
			mav.addObject("errCode", 1);	// not exist userId in database
			mav.setViewName("/lmAdm/adm_login");			
			return mav; 
		}else{
			System.out.println(loginCheckResult.getMe_name());
			System.out.println(loginCheckResult.getMe_level());
			
			
			
			if(loginCheckResult.getMe_level() == 1 || loginCheckResult.getMe_level() == 2){
				
				System.out.println("로그인성공");
				session.setAttribute("SITE_UID", uid);
				session.setAttribute("SITE_NAME", loginCheckResult.getMe_name());
				session.setAttribute("SITE_LEVEL", loginCheckResult.getMe_level());
				session.setAttribute("SITE_EMAIL", loginCheckResult.getMe_email());
				//mav.setViewName("redirect:/lmAdm/index.do");
				mav.setViewName("redirect:/lmAdm/index.do");
				return mav;     
				
				
			}else{
				System.out.print("로그인실패 레벨 안맞음");
				mav.addObject("errCode", 2);
				mav.setViewName("/lmAdm/adm_login");			
				return mav; 
				
			}  
		} 
		
	}//end method
	
	
	
	@RequestMapping("/logout.do")
	public ModelAndView logout(HttpSession session){
		session.invalidate();
		ModelAndView mav = new ModelAndView();
		//mav.addObject("errCode", 3);//로그아웃 문구
		mav.setViewName("redirect:/lmAdm/login.do");
		return mav;
	}
	
	
}










